﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using HappyTravelling.DataModel;
using HappyTravelling.ThanhService;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using Newtonsoft.Json;

namespace HappyTravelling.Views
{
    public partial class LoginPage : PhoneApplicationPage
    {
        private WebService1SoapClient client;
        public LoginPage()
        {
            InitializeComponent();
        }

        void client_LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            int loginRetCode = (int)e.Result;
            if (loginRetCode > 0)
            {
                //MessageBox.Show("Nhảy vào thành công nhá :)) ret code: " + loginRetCode);
                client.GetUserByIdAsync(loginRetCode);
                client.GetUserByIdCompleted += client_GetUserByIdCompleted;
                
            }
            else
            {
                MessageBox.Show("Định hack anh à :v, ret code: " +loginRetCode);
            }
        }

        void client_GetUserByIdCompleted(object sender, GetUserByIdCompletedEventArgs e)
        {
            try
            {
                string json = e.Result;
                App.CurrentUser = JsonConvert.DeserializeObject<User>(json);
                NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private void btLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                client = new WebService1SoapClient();
                client.LoginAsync(tbAccount.Text, tbPassword.Password);
                client.LoginCompleted += client_LoginCompleted;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
                // sao ky cuc a?ah, ke no, e chua sua cai nay, a cu lam cai ia di
        }

        private void btSignin_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddAccount.xaml", UriKind.Relative));
        }

    }
}